cap log close

global path = "****"

local name= "excl_restriction"
local date = "061720"
global logname = "$path/`name'_`date'.log"

clear
set more off
set matsize 11000
cd "$path"
log using "$logname", replace


use Data/tax_liab_2017,clear
sort tin -cycle_pst
bys tin:keep if _n==1
rename penalty penalty17
gen inctax17=inctxc-eicc-addlchc+penalty-add_ptc
rename adj_incc income17
keep tin inctax17 penalty17 income17
save temp1,replace

use Data/tax_liab_2018,clear
sort tin -cycle_pst
bys tin:keep if _n==1
rename penalty penalty18
gen inctax18=inctxc-eicc-addlchc+penalty-add_ptc
rename adj_incc income18
keep tin inctax18 penalty18 income18
merge 1:1 tin using temp1,nogen 
save temp1,replace


use Data/w2_2017,clear
collapse (sum) wages withhldg,by(tin)
rename wages wages17
rename withhldg withhldg17
merge 1:1 tin using temp1,nogen 
save temp1,replace

use Data/w2_2018,clear
collapse (sum) wages withhldg,by(tin)
rename wages wages18
rename withhldg withhldg18
merge 1:1 tin using temp1,nogen 
save temp1,replace

use Data/f1095_2017,clear
collapse (sum) aptctotal slcsptotal premtotal,by(tin)
foreach var of varlist aptctotal slcsptotal premtotal{
rename `var' `var'17
}
merge 1:1 tin using temp1,nogen 
save temp1,replace

merge 1:1 tin using Data/agi2016,nogen
save temp1,replace

use Data/f1095_2018,clear
collapse (sum) aptctotal slcsptotal premtotal,by(tin)
foreach var of varlist aptctotal slcsptotal premtotal{
rename `var' `var'18
}
merge 1:1 tin using temp1,nogen 
save temp1,replace

merge 1:m tin using Data/penalty_sample 

replace age_2017=0 if age_2017<0

replace inctax17=withhldg17 if inctax17==.&withhldg17~=.
replace inctax18=withhldg18 if inctax18==.&withhldg18~=.

foreach var of varlist inctax17 penalty17 income17 inctax18 penalty18 income18 wages17 wages18 withhldg17 withhldg18 aptctotal17 aptctotal18 ///
slcsptotal17 slcsptotal18 premtotal17 premtotal18{
replace `var'=0 if `var'==.
}

gen netprem17=max(premtotal17-aptctotal17,0)
gen netprem18=max(premtotal18-aptctotal18,0)

foreach var of varlist inctax17 penalty17 income17 inctax18 penalty18 income18 wages17 wages18 withhldg17 withhldg18 aptctotal17 aptctotal18 ///
slcsptotal17 slcsptotal18 premtotal17 premtotal18 netprem17 netprem18 agi2016{
gen `var'w=`var'
qui sum `var' if `var'!=0,d
replace `var'w=r(p99) if `var'>r(p99)&`var'!=0
replace `var'w=r(p1) if `var'<r(p1)&`var'!=0

}



*sample restriction
keep if notall16 == 1
keep if age_2017 < 65 & age_2017 >= 45


rename fpl_frac magi_fpl2015
gen penalty_avg_w = (penalty17w+penalty18w)/2

*penalty paid in 2017 
reg penalty_avg_w treatment, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)
estimates save "$path/exclusion_rest_`date'.ster", replace

*effect of intervention on any 2017 esi coverage
reg any_esi2017 treatment, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/exclusion_rest_`date'.ster", append

*effect of intervention on any 2018 esi coverage
reg any_esi2018 treatment, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/exclusion_rest_`date'.ster", append

*effect of intervention on 2017 mortality
reg dead2017 treatment, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/exclusion_rest_`date'.ster", append

			
log close


